<template>
    <div style="padding:10px">
        <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem label="公文状态" prop="value">
                <RadioGroup v-model="form.value">
                    <Radio label="yes">通过</Radio>
                    <Radio label="no">不通过</Radio>
                </RadioGroup>
            </FormItem>
            <div style="margin-bottom:23px" v-if="form.value=='yes'">
                <p style="margin-bottom:10px">批示：</p>
                <Input v-model="form.approval.zh" type="textarea" :rows="4" placeholder="输入批示..."></Input>
            </div>
            <div style="margin-bottom:23px" v-else-if="form.value=='no'">
                <p style="margin-bottom:10px">意见：</p>
                <Input v-model="form.content.zh" type="textarea" :rows="4" placeholder="输入意见..."></Input>

            </div>
            <div v-if="form.value=='no_sure' || form.value=='yes'">
                <FormItem label="选择查阅人" prop="teacher">
                    <Input v-model="exnameteacher" @on-focus="modalteacher=true" placeholder="选择查阅人" type="textarea"
                           :autosize="{minRows: 2,maxRows: 5}"></Input>
                </FormItem>
            </div>
            <Button type="primary" @click="moralAdd" long :loading="loading">提交</Button>
        </Form>
        <Modal v-model="modalteacher" title="选择查阅人" @on-cancel="cancelstatus" width="350">
            <div style="text-align:center;margin-bottom:10px">
                <RadioGroup v-model="partype" type="button" size="small" @on-change="ptchange">
                    <Radio label="dus">职务</Radio>
                    <Radio label="dep">所有人</Radio>
                </RadioGroup>
            </div>
            <Input v-model="seartxt" size="small" placeholder="搜索人员" @on-change="toSearch(seartxt)"
                   v-if="partype=='dep'" style="width: 300px;"/>
            <Select v-model="dutie_id" v-if="partype=='dus'" size="small" @on-change="chax" placeholder="请选择职务">
                <Option :value="item.value" v-for="(item,ind) in duties" :key="ind">{{item.title.zh}}</Option>
            </Select>
            <div style="min-height:250px;margin-top:10px;height:400px;overflow:auto">
                <CheckboxGroup v-model="form.teacher" @on-change="toselec">
                    <div v-for="item in wonima" :key="item.user_id" style="margin-bottom:2px">
                        <Checkbox :label="item.user_id">
                            {{item.teacher_name.zh||item.teacher_name.mn}}
                        </Checkbox>
                    </div>
                </CheckboxGroup>
            </div>
        </Modal>
    </div>
</template>

<script>
    export default {

        components: {},

        props: {
            id: Number,
            userId: Number,
        },

        data() {
            return {
                getypeurl: "api/dictionary/v1/dictionary/pidquerys?document_type",
                auditUrl: 'api/document/v1/document/update_examine/',
                form: {
                    value: '',
                    approval: {
                        zh: '',
                        en: '',
                        mn: '',
                    },
                    content: {
                        zh: '',
                        en: '',
                        mn: '',
                    },
                    teacher: [],
                },
                partype: 'dus',
                dutie_id: '',
                seartxt: '',
                duties: [],

                loading: false,
                modal: false,
                modalteacher: false,
                rules: {
                    value: [
                        {required: true, message: '请审核', trigger: 'change'}
                    ],
                    'teacher': [
                        {required: true, type: 'array', message: '至少选择一个传阅人', trigger: 'change',}
                    ],
                },
                examiners: [],
                examiners_copy: [],
                wonima: [],
                // 上传路径
                upload_url: this.$api + '/api/user/v1/editor/upload?module=documents',
                upload_token: localStorage.token,
            }
        },
        methods: {
            toselec(tp) {
                this.$emit('on-select', tp)
            },
            ptchange(type) {
                if (type == 'dep') {
                    this.wonima = this.examiners_copy
                } else {
                    this.wonima = []
                    this.dutie_id = ''
                }
            },
            chax(id) {
                let url = `api/user/v1/get/teacher/user/list?duties_id=[${id}]`
                this.$http.get(url).then(res => {
                    this.wonima = res.data
                }).catch(
                    err => this.$Message.error('人员获取失败！')
                )
            },
            // 搜索人员
            toSearch(txt) {
                if (txt.length != 0) {
                    this.wonima = this.examiners_copy.filter(item => {
                        return new RegExp(txt).test(item.teacher_name)
                    })
                } else {
                    this.wonima = this.examiners_copy
                }
            },
            zwload() {
                this.$http.get('api/user/v1/get/duties/list').then(res => {
                    this.duties = res.data.map(item => ({
                        value: item.id,
                        title: item.duties_name.zh ? item.duties_name.zh : item.duties_name.mn
                    }))
                }).catch(
                    err => this.$Message.error('职务加载失败！')
                )
            },
            cancelstatus() {
                this.form.teacher = []
            },
            load() {
                this.$http.get('api/document/v1/document/getone_examine/' + this.id)
                    .then(res => {

                        if (res.data.status == 1) {
                            this.form.value = 'yes'
                            this.form.approval = res.data.approval.approval
                            if (res.data.teacher.length > 0) {
                                this.form.teacher = []
                                res.data.teacher.forEach(el => {
                                    this.form.teacher.push(el.user_id)
                                })
                            }
                        } else if (res.data.status == 0) {
                            this.form.value = 'no'
                            this.form.content = res.data.content
                        }

                    }, err => console.log(err))
            },
            panHide() {
                this.$emit('close')
                this.$emit('reload')
            },
            // 点击添加按钮将数据发送给后端
            moralAdd() {
                if (this.form.value == 'yes') {
                    delete this.form.content
                } else {
                    delete this.form.teacher
                    delete this.form.approval
                }
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading = true
                        this.$http.put(this.auditUrl + this.id, this.form).then(val => {
                            if (val.code == 200) {
                                this.panHide();
                                this.loading = false
                            } else {
                                this.loading = false
                            }
                        })
                    } else {
                        this.$Message.error('验证失败！');
                    }
                })
            },
            getExamine() {
                this.$http.get('api/user/v1/get/teacher/user/list').then(val => {
                    // val.data.splice(val.data.findIndex(item=>item.user_id==this.userId),1)
                    this.examiners = val.data
                    this.wonima = []
                    this.examiners_copy = val.data
                }).catch(
                    err => console.log(err)
                )
            },
        },
        computed: {
            exnameteacher() {
                if (this.form.teacher.length > 0) {
                    let arrar = []
                    this.form.teacher.forEach(el => {
                        this.examiners.forEach(item => {
                            if (el == item.user_id) {
                                arrar.push(item.teacher_name.zh ? item.teacher_name.zh : item.teacher_name.mn)
                            }
                        })
                    })
                    return arrar.join(',')
                }
            },

        },
        created() {
            this.load()
            this.getExamine()
            this.zwload()
        },
        watch: {}
    }
</script>

<style scoped>

</style>
